package com.skt.common_hr.overtime.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.overtime.model.vo.MemberOtVO;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class MemberOtProxy extends CommonProxy
	{
		public static const NAME:String = "MemberOtProxy";
		public static const LOAD_MEMBER_OT_COMPLETE:String 	= "loadMemberOtComplete";
		public static const LOAD_MEMBER_OT_FAILED:String 	= "loadMemberOtFailed";
		public static const SAVE_MEMBER_OT_COMPLETE:String 	= "saveMemberOtComplete";
		public static const SAVE_MEMBER_OT_FAILED:String 	= "saveMemberOtFailed";
		
		public var list:ArrayCollection;
		public var orgeh:String;
		
		
		public function MemberOtProxy(data:Object=null)
		{
			super(NAME, data);
		}


		//병원목록
		public function load(d:Date, orgeh:String):void
		{
			
			var params:Object = new Object();
			params.I_DATE = DateUtil.toBasicString(d);
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgeh;
			params.I_OPERATION = "DIS";
			
			loadData("Z_HR_0115", params, listResult, listFault);
		}
		
		public function save(d:Date, orgeh:String, otList:Array):void
		{
			var params:Object = new Object();
			params.I_DATE = DateUtil.toBasicString(d);
			params.I_PERNR = Variables.myPernr;
			params.I_ORGEH = orgeh;
			params.I_OPERATION = "MOD";
			
			var table:Object = new Object();
			table["T_OUT"] = otList;
			saveData("Z_HR_0115", params, table, saveResult, saveFault);
		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				list = parseList(event.result.T_OUT);
				facade.sendNotification(LOAD_MEMBER_OT_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_MEMBER_OT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function listFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_MEMBER_OT_FAILED, this);
		}
		
		
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				facade.sendNotification(SAVE_MEMBER_OT_COMPLETE, this);
			}
			else {
				//Alert.show("["+retText+"]");
				
				//2014.0212 김선우과장 요청 
				facade.sendNotification(SAVE_MEMBER_OT_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function saveFault(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_MEMBER_OT_FAILED, this);
		}
		
		
		
		
		
		
		
		
		private function parseList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection;
			for each(var rec:Object in src) {
				var vo:MemberOtVO = new MemberOtVO;
				vo.PERNR = StringUtil.trimFront(rec.PERNR, "0");
				vo.ENAME = rec.ENAME;
				vo.OTWOK = rec.OTWOK;
				vo.TEXT1 = rec.TEXT1;
				vo.VACAT = rec.VACAT;
				vo.BTRIP = rec.BTRIP;
				vo.ZLATE = rec.ZLATE;
				vo.JIKCHAK = rec.JIKCHAK;
				list.addItem(vo);
			}
			return list;
		}	
	}
}